package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.SuiPian.HelpFragment;
import com.example.myapplication.SuiPian.HomeFragment;
import com.example.myapplication.SuiPian.NewsFragment;
import com.example.myapplication.SuiPian.PencenFragment;
import com.example.myapplication.SuiPian.ServlerFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView botview;
    private HelpFragment helpFragment;
    private HomeFragment homeFragment;
    private ServlerFragment
            servlerFragment;
    private PencenFragment pencenFragment;
    private NewsFragment newsFragment;

    private BotviewReceiver botviewReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        botview = findViewById(R.id.botview);
        helpFragment = new HelpFragment();
        homeFragment = new HomeFragment();
        newsFragment = new NewsFragment();
        pencenFragment = new PencenFragment();
        servlerFragment = new ServlerFragment();

        //动态注册
        botviewReceiver = new BotviewReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("botviewReceiver_action");
        filter.addAction("PersonalInfoActivity_action");
        registerReceiver(botviewReceiver, filter);


            getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, homeFragment).commit();
        botview.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.home:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, homeFragment).commit();
                        break;
                    case R.id.help:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, helpFragment).commit();
                        break;
                    case R.id.servler:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, servlerFragment).commit();
                        break;
                    case R.id.news:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, newsFragment).commit();
                        break;
                    case R.id.percen:
                        getSupportFragmentManager().beginTransaction().replace(R.id.YeMian, pencenFragment).commit();
                        break;
                }
                return true;
            }
        });
    }


    //第一步，写一个广播接收器
    //2:动态注册
    //第三步，发送广播
    public class BotviewReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
             Log.v("fwling22","onReceive");
            if("botviewReceiver_action".equals(action)){
                botview.setSelectedItemId(R.id.help);
            }else if("PersonalInfoActivity_action".equals(action)){
                Log.v("fwling22","botviewReceiver_action1");
//                botview.setSelectedItemId(R.id.help);
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
         unregisterReceiver(botviewReceiver);
    }

}